import QtQuick 2.12
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.12

Rectangle {
    color: "transparent"

    // --------------------- 左侧导航栏 ---------------------
    Rectangle {
        id: navBar
        width: 180
        height: parent.height
        color: "#f8f9fa"
        border.color: "#dee2e6"
        radius: 8

        Column {
            anchors.fill: parent
            anchors.margins: 10
            spacing: 8

            // 操作人员信息管理
            Button {
                text: "操作人员信息管理"
                width: parent.width - 20
                height: 45
                background: Rectangle {
                    color: userListView.visible ? "#3498db" : "#ecf0f1"
                    border.color: userListView.visible ? "#2980b9" : "#bdc3c7"
                    border.width: userListView.visible ? 2 : 1
                    radius: 6
                }
                contentItem: Text {
                    text: "操作人员信息管理"
                    color: userListView.visible ? "white" : "#2c3e50"
                    font.pixelSize: 13
                    font.bold: userListView.visible
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                }
                onClicked: {
                    userListView.visible = true
                    machineListView.visible = false
                }
            }

            // 农机履历信息管理
            Button {
                text: "农机履历信息管理"
                width: parent.width - 20
                height: 45
                background: Rectangle {
                    color: machineListView.visible ? "#3498db" : "#ecf0f1"
                    border.color: machineListView.visible ? "#2980b9" : "#bdc3c7"
                    border.width: machineListView.visible ? 2 : 1
                    radius: 6
                }
                contentItem: Text {
                    text: "农机履历信息管理"
                    color: machineListView.visible ? "white" : "#2c3e50"
                    font.pixelSize: 13
                    font.bold: machineListView.visible
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                }
                onClicked: {
                    userListView.visible = false
                    machineListView.visible = true
                }
            }
        }
    }

    // --------------------- 右侧内容区域 ---------------------
    Rectangle {
        x: navBar.width + 10
        width: parent.width - navBar.width - 10
        height: parent.height
        color: "white"
        border.color: "#dee2e6"
        radius: 8

        // 操作人员列表
        ListView {
            id: userListView
            anchors.fill: parent
            anchors.margins: 15
            visible: true // 初始显示
            clip: true

            model: ListModel {
                ListElement { userName: "admin_01"; createTime: "2024-01-15 09:30:00" }
                ListElement { userName: "admin_02"; createTime: "2024-01-16 14:20:00" }
                ListElement { userName: "admin_03"; createTime: "2024-01-17 11:45:00" }
                ListElement { userName: "admin_04"; createTime: "2024-01-18 16:10:00" }
                ListElement { userName: "operator_01"; createTime: "2024-01-19 08:15:00" }
                ListElement { userName: "operator_02"; createTime: "2024-01-20 13:25:00" }
            }

            delegate: Item {
                width: parent.width
                height: 70

                Rectangle {
                    anchors.fill: parent
                    anchors.margins: 5
                    border.color: "#e9ecef"
                    color: index % 2 === 0 ? "#f8f9fa" : "white"
                    radius: 6

                    RowLayout {
                        anchors.fill: parent
                        anchors.margins: 15
                        spacing: 20

                        ColumnLayout {
                            Layout.fillWidth: true
                            spacing: 5

                            Text {
                                text: "用户: " + userName
                                font.pixelSize: 15
                                font.bold: true
                                color: "#2c3e50"
                            }

                            Text {
                                text: "创建时间: " + createTime
                                font.pixelSize: 12
                                color: "#6c757d"
                            }
                        }

                        Button {
                            text: "修改账号信息"
                            Layout.preferredWidth: 120
                            Layout.preferredHeight: 35
                            background: Rectangle {
                                color: "#3498db"
                                radius: 6
                            }
                            contentItem: Text {
                                text: "修改账号信息"
                                color: "white"
                                font.pixelSize: 12
                                horizontalAlignment: Text.AlignHCenter
                                verticalAlignment: Text.AlignVCenter
                            }
                            onClicked: {
                                modifyDialog.currentUserName = userName  // 传递当前用户名
                                modifyDialog.open()
                            }
                        }
                    }
                }
            }

            ScrollBar.vertical: ScrollBar {
                policy: ScrollBar.AsNeeded
            }
        }

        // 农机列表
        ListView {
            id: machineListView
            anchors.fill: parent
            anchors.margins: 15
            visible: false // 初始隐藏
            clip: true

            model: ListModel {
                ListElement { machineId: "GZJY001"; createTime: "2024-01-10 08:00:00"; status: "正常" }
                ListElement { machineId: "GZJY002"; createTime: "2024-01-12 09:30:00"; status: "正常" }
                ListElement { machineId: "GZJY003"; createTime: "2024-01-14 10:15:00"; status: "维修中" }
                ListElement { machineId: "GZJY004"; createTime: "2024-01-16 11:45:00"; status: "正常" }
                ListElement { machineId: "GZJY005"; createTime: "2024-01-18 14:20:00"; status: "待检修" }
            }

            delegate: Item {
                width: parent.width
                height: 70

                Rectangle {
                    anchors.fill: parent
                    anchors.margins: 5
                    border.color: "#e9ecef"
                    color: index % 2 === 0 ? "#f8f9fa" : "white"
                    radius: 6

                    RowLayout {
                        anchors.fill: parent
                        anchors.margins: 15
                        spacing: 20

                        ColumnLayout {
                            Layout.fillWidth: true
                            spacing: 5

                            RowLayout {
                                spacing: 10

                                Text {
                                    text: "机器: " + machineId
                                    font.pixelSize: 15
                                    font.bold: true
                                    color: "#2c3e50"
                                }

                                Rectangle {
                                    width: 8
                                    height: 8
                                    radius: 4
                                    color: status === "正常" ? "#28a745" :
                                            status === "维修中" ? "#ffc107" : "#dc3545"
                                }

                                Text {
                                    text: status
                                    font.pixelSize: 12
                                    color: status === "正常" ? "#28a745" :
                                            status === "维修中" ? "#ffc107" : "#dc3545"
                                    font.bold: true
                                }
                            }

                            Text {
                                text: "创建时间: " + createTime
                                font.pixelSize: 12
                                color: "#6c757d"
                            }
                        }

                        Button {
                            text: "查看机器信息"
                            Layout.preferredWidth: 120
                            Layout.preferredHeight: 35
                            background: Rectangle {
                                color: "#17a2b8"
                                radius: 6
                            }
                            contentItem: Text {
                                text: "查看机器信息"
                                color: "white"
                                font.pixelSize: 12
                                horizontalAlignment: Text.AlignHCenter
                                verticalAlignment: Text.AlignVCenter
                            }
                            onClicked: {
                                machineDetailDialog.machineId = machineId
                                machineDetailDialog.machineStatus = status
                                machineDetailDialog.open()
                            }
                        }
                    }
                }
            }

            ScrollBar.vertical: ScrollBar {
                policy: ScrollBar.AsNeeded
            }
        }
    }

    // --------------------- 修改账号信息弹窗 ---------------------
    Dialog {
        id: modifyDialog
        title: "修改账号信息"
        width: 550
        height: 420
        modal: true
        closePolicy: Dialog.CloseOnEscape | Dialog.CloseOnClickOutside
        property string currentUserName: ""  // 存储当前修改的用户名

        ColumnLayout {
            anchors.fill: parent
            anchors.margins: 20
            spacing: 15

            RowLayout {
                Layout.fillWidth: true
                spacing: 10

                Label {
                    text: "用户名:"
                    font.pixelSize: 14
                    color: "#2c3e50"
                    width: 80
                }

                TextField {
                    id: userNameField
                    text: modifyDialog.currentUserName  // 绑定当前用户名
                    Layout.fillWidth: true
                    font.pixelSize: 14
                    background: Rectangle {
                        border.color: "#ced4da"
                        border.width: 1
                        radius: 4
                        color: "white"
                    }
                }
            }

            RowLayout {
                Layout.fillWidth: true
                spacing: 10

                Label {
                    text: "账号:"
                    font.pixelSize: 14
                    color: "#2c3e50"
                    width: 80
                }

                TextField {
                    id: accountField
                    Layout.fillWidth: true
                    font.pixelSize: 14
                    background: Rectangle {
                        border.color: "#ced4da"
                        border.width: 1
                        radius: 4
                        color: "white"
                    }
                }
            }

            RowLayout {
                Layout.fillWidth: true
                spacing: 10

                Label {
                    text: "密码:"
                    font.pixelSize: 14
                    color: "#2c3e50"
                    width: 80
                }

                TextField {
                    id: passwordField
                    echoMode: TextField.Password
                    Layout.fillWidth: true
                    font.pixelSize: 14
                    background: Rectangle {
                        border.color: "#ced4da"
                        border.width: 1
                        radius: 4
                        color: "white"
                    }
                }

                Button {
                    text: "修改密码"
                    Layout.preferredWidth: 100
                    background: Rectangle {
                        color: "#ffc107"
                        radius: 4
                    }
                    contentItem: Text {
                        text: "修改密码"
                        color: "#212529"
                        font.pixelSize: 12
                        horizontalAlignment: Text.AlignHCenter
                    }
                    onClicked: console.log("触发修改密码逻辑")
                }
            }

            RowLayout {
                Layout.fillWidth: true
                spacing: 10

                Label {
                    text: "权限级别:"
                    font.pixelSize: 14
                    color: "#2c3e50"
                    width: 80
                }

                ComboBox {
                    id: permissionCombo
                    model: ["管理员", "操作员", "观察员"]
                    currentIndex: 0
                    Layout.fillWidth: true
                    font.pixelSize: 14
                    background: Rectangle {
                        border.color: "#ced4da"
                        border.width: 1
                        radius: 4
                        color: "white"
                    }
                }
            }

            RowLayout {
                Layout.fillWidth: true
                spacing: 10

                Label {
                    text: "农田:"
                    font.pixelSize: 14
                    color: "#2c3e50"
                    width: 80
                }

                ComboBox {
                    id: farmCombo
                    model: ["一号农田", "二号农田", "三号农田", "四号农田"]
                    currentIndex: 0
                    Layout.fillWidth: true
                    font.pixelSize: 14
                    background: Rectangle {
                        border.color: "#ced4da"
                        border.width: 1
                        radius: 4
                        color: "white"
                    }
                }
            }

            RowLayout {
                Layout.alignment: Qt.AlignRight
                spacing: 15

                Button {
                    text: "取消"
                    Layout.preferredWidth: 80
                    background: Rectangle {
                        color: "#6c757d"
                        radius: 4
                    }
                    contentItem: Text {
                        text: "取消"
                        color: "white"
                        font.pixelSize: 13
                        horizontalAlignment: Text.AlignHCenter
                    }
                    onClicked: modifyDialog.close()
                }

                Button {
                    text: "确认修改"
                    Layout.preferredWidth: 100
                    background: Rectangle {
                        color: "#28a745"
                        radius: 4
                    }
                    contentItem: Text {
                        text: "确认修改"
                        color: "white"
                        font.pixelSize: 13
                        horizontalAlignment: Text.AlignHCenter
                    }
                    onClicked: {
                        console.log("确认修改：用户名=" + modifyDialog.currentUserName +
                            "，账号=" + accountField.text)
                        modifyDialog.close()
                    }
                }
            }
        }
    }

    // --------------------- 机器详细信息弹窗 ---------------------
    Dialog {
        id: machineDetailDialog
        title: "机器详细信息 - " + machineId
        width: 600
        height: 400
        modal: true
        closePolicy: Dialog.CloseOnEscape | Dialog.CloseOnClickOutside
        property string machineId: ""
        property string machineStatus: ""
        property string errorString: ""

        contentItem: ColumnLayout {
            anchors.fill: parent
            spacing: 15

            Text {
                text: "机器 " + parent.machineId + " 的维保信息"
                font.pixelSize: 16
                font.bold: true
                color: "#2c3e50"
                Layout.alignment: Qt.AlignHCenter
            }

            // 维保记录表格
            Rectangle {
                Layout.fillWidth: true
                Layout.fillHeight: true
                border.color: "#dee2e6"
                border.width: 1
                radius: 6

                ColumnLayout {
                    anchors.fill: parent
                    anchors.margins: 15
                    spacing: 10

                    // 表头
                    RowLayout {
                        Layout.fillWidth: true
                        spacing: 10

                        Text {
                            text: "机器人ID"
                            font.pixelSize: 13
                            font.bold: true
                            color: "#2c3e50"
                            Layout.preferredWidth: 100
                        }

                        Text {
                            text: "上次维保"
                            font.pixelSize: 13
                            font.bold: true
                            color: "#2c3e50"
                            Layout.preferredWidth: 120
                        }

                        Text {
                            text: "下次维保"
                            font.pixelSize: 13
                            font.bold: true
                            color: "#2c3e50"
                            Layout.preferredWidth: 120
                        }

                        Text {
                            text: "状态"
                            font.pixelSize: 13
                            font.bold: true
                            color: "#2c3e50"
                            Layout.fillWidth: true
                        }
                    }

                    Rectangle {
                        height: 1
                        Layout.fillWidth: true
                        color: "#dee2e6"
                    }

                    // 表格内容
                    ScrollView {
                        Layout.fillWidth: true
                        Layout.fillHeight: true
                        clip: true

                        ListView {
                            width: parent.width
                            model: ListModel {
                                ListElement { robotId: "GZJY001"; lastMaintain: "2024-09-01"; nextMaintain: "2024-12-01"; status: "正常" }
                                ListElement { robotId: "GZJY002"; lastMaintain: "2024-08-15"; nextMaintain: "2024-11-15"; status: "正常" }
                                ListElement { robotId: "GZJY003"; lastMaintain: "2024-07-20"; nextMaintain: "2024-10-20"; status: "待检修" }
                            }

                            delegate: Rectangle {
                                width: parent.width
                                height: 50
                                color: index % 2 === 0 ? "#f8f9fa" : "white"

                                RowLayout {
                                    anchors.fill: parent
                                    anchors.margins: 10
                                    spacing: 10

                                    Text {
                                        text: robotId
                                        font.pixelSize: 13
                                        color: "#2c3e50"
                                        Layout.preferredWidth: 100
                                    }

                                    Text {
                                        text: lastMaintain
                                        font.pixelSize: 13
                                        color: "#2c3e50"
                                        Layout.preferredWidth: 120
                                    }

                                    Text {
                                        text: nextMaintain
                                        font.pixelSize: 13
                                        color: "#2c3e50"
                                        Layout.preferredWidth: 120
                                    }

                                    RowLayout {
                                        spacing: 5
                                        Layout.fillWidth: true

                                        Rectangle {
                                            width: 8
                                            height: 8
                                            radius: 4
                                            color: status === "正常" ? "#28a745" :
                                                    status === "维修中" ? "#ffc107" : "#dc3545"
                                        }

                                        Text {
                                            text: status
                                            font.pixelSize: 13
                                            color: status === "正常" ? "#28a745" :
                                                    status === "维修中" ? "#ffc107" : "#dc3545"
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        // 弹窗底部按钮
        footer: RowLayout {
            anchors.horizontalCenter: parent.horizontalCenter
            spacing: 15

            Button {
                text: "关闭"
                Layout.preferredWidth: 80
                background: Rectangle { color: "#6c757d"; radius: 4 }
                contentItem: Text {
                    text: "关闭"
                    color: "white"
                    font.pixelSize: 13
                    horizontalAlignment: Text.AlignHCenter
                }
                onClicked: machineDetailDialog.close()
            }
        }
    }
}